package com.swpu.service_weight.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_weight.entity.WeightPlan;
import com.swpu.service_weight.mapper.WeightPlanMapper;
import com.swpu.service_weight.service.WeightPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-04-10
 */
@Service
public class WeightPlanServiceImpl extends ServiceImpl<WeightPlanMapper, WeightPlan> implements WeightPlanService {

    @Override
    public void updateUserWeightPlan(String openid, WeightPlan weightPlan) {
        LambdaQueryWrapper<WeightPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeightPlan::getUserId,openid);
        WeightPlan plan = this.getOne(queryWrapper);
        if(plan==null){
            weightPlan.setStartDate(new Date());
            weightPlan.setUserId(openid);
            this.save(weightPlan);
        }else {
            this.updateById(weightPlan);
        }

    }

    @Override
    public WeightPlan getUserWeightPlan(String openid) {
        LambdaQueryWrapper<WeightPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeightPlan::getUserId,openid);
        WeightPlan weightPlan = this.getOne(queryWrapper);
        return weightPlan;
    }
}
